package cn.stylefeng.guns.modular.robot.cmd.impl;

import cn.stylefeng.guns.modular.robot.cmd.abs.AbstractCmd;
import cn.stylefeng.guns.modular.robot.entity.Group;
import cn.stylefeng.guns.modular.robot.entity.OrderBatch;
import cn.stylefeng.guns.modular.robot.entity.enums.GroupMode;
import cn.stylefeng.guns.modular.robot.service.GroupService;
import cn.stylefeng.guns.modular.robot.service.OrderBatchService;
import cn.stylefeng.guns.modular.robot.utils.RobotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class CmdStart extends AbstractCmd {

    @Resource
    private GroupService groupService;
    @Resource
    private OrderBatchService orderBatchService;

    @Override
    public void exec(Update update, TelegramLongPollingBot bot) {
        Long chatId = update.getMessage().getChatId();
        List<Group> groups = groupService.lambdaQuery()
                .eq(Group::getTgGroupId, chatId)
                .list();
        if (CollectionUtils.isEmpty(groups)) {
            RobotUtils.sendText(chatId, "群未在系统维护，开启群账单失败！", bot);
        }
        Group group = groups.get(0);
        if (Boolean.TRUE.equals(group.getBatchStatus())) {
            RobotUtils.sendText(chatId, "记账已开启，无需开启！", bot);
            return;
        }
        OrderBatch orderBatch = new OrderBatch();
        orderBatch.setBatchId(UUID.randomUUID().toString().replace("-", ""));
        orderBatch.setRate(group.getRate());
        orderBatch.setExchangeRate(group.getExchangeRate());
        orderBatch.setGroupId(group.getTgGroupId());
        orderBatch.setUnit(group.getUnit());
        orderBatch.setExtract(group.getExtract());
        orderBatch.setGroupMode(group.getGroupMode());
        orderBatch.setStartTime(new Date());
        orderBatchService.save(orderBatch);
        group.setBatchId(orderBatch.getBatchId());
        group.setBatchStartTime(new Date());
        group.setBatchStatus(true);
        groupService.updateById(group);
        sendUMsg(chatId, group, bot);
        sendBiMsg(chatId, group, bot);
    }

    private void sendBiMsg(Long chatId, Group group, TelegramLongPollingBot bot) {
        if (GroupMode.USDT.equals(group.getGroupMode())) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        String format = String.format("%.0f", group.getExchangeRate());
        sb.append("当前汇率为").append(format).append("新账单为币本位模式。\n");
        sb.append("本模式只以 " + group.getUnit() + " 作为计算。\n");
        sb.append("------\n");
        sb.append("当前模式 币本位计算\n");
        sb.append("当前汇率 ").append(format).append("\n");
        sb.append("------\n");
        sb.append("输入命令\"结束\"可以结束本轮记账");
        RobotUtils.sendText(chatId, sb.toString(), bot);
    }

    private void sendUMsg(Long chatId, Group group, TelegramLongPollingBot bot) {
        if (GroupMode.币.equals(group.getGroupMode())) {
            return;
        }
        String format = String.format("%.0f", group.getExchangeRate());
        StringBuilder sb = new StringBuilder();
        sb.append("当前汇率为").append(format).append("新账单为U本位模式。\n");
        sb.append("本模式所有账单均以USD汇率换算。\n");
        sb.append("------\n");
        sb.append("当前模式 U本位计算\n");
        sb.append("当前汇率 ").append(format).append("\n");
        sb.append("------\n");
        sb.append("输入命令\"结束\"可以结束本轮记账");
        RobotUtils.sendText(chatId, sb.toString(), bot);
    }

    @Override
    public String getCmdName() {
        return "开始";
    }

    @Override
    public String getDescribe() {
        return "开始                           开始记账批次\n";
    }
}
